using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using BoOnce.Components;

namespace BoOnce.Graphics
{
    class LineDebuger
    {
        private Texture2D _Pixel;
        private List<Line> _Lines;
        private SpriteBatch _SpriteBatch;
        private CameraComponent _Camera;

        public LineDebuger(Game game)
        {
            _Camera = (CameraComponent)game.Services.GetService(typeof(CameraComponent));
            _Pixel = new Texture2D(game.GraphicsDevice, 1, 1, 1, TextureUsage.None, SurfaceFormat.Color);
            Color[] pixels = new Color[1];
            pixels[0] = Color.White;
            _Pixel.SetData<Color>(pixels);
            _Lines = new List<Line>();
            _SpriteBatch = new SpriteBatch(game.GraphicsDevice);
        }

        public void Add(Line line)
        {
            _Lines.Add(line);
        }

        public void Clear()
        {
            _Lines.Clear();
        }

        public void Draw()
        {
            _SpriteBatch.Begin();
            foreach (Line line in _Lines)
            {
                // calculate the distance between the two vectors 
                float distance = Vector2.Distance(line.Start, line.Stop);
                // calculate the angle between the two vectors
                float angle = (float)Math.Atan2((double)(line.Stop.Y - line.Start.Y),
                    (double)(line.Stop.X - line.Start.X));
                // stretch the pixel between the two vectors                
                _SpriteBatch.Draw(_Pixel,
                    _Camera.ToScreenPosition(line.Start),
                    null,
                    line.Color,
                    angle,
                    Vector2.Zero,
                    new Vector2(distance, 1),
                    SpriteEffects.None,
                    0);
            }
            _SpriteBatch.End();
            _Lines.Clear();
        }
    }

    class Line
    {
        public Vector2 Start;
        public Vector2 Stop;
        public Color Color;

        public Line(Vector2 start, Vector2 stop, Color color)
        {
            Start = start;
            Stop = stop;
            Color = color;
        }
    }
}
